/*
{
"状态":1,
"名称":"新增-用户资料",
"作者":"陈泽椿",
"创建时间":"2018-06-30",
"功能":"新增-用户资料",
"log": [
	{"date":"06-01" , "dev" :"hzhh" , "content" : ""}
]
}
*/
declare @company_key int ,@user_id varchar(100) , @user_name varchar(100) , @sex varchar(100)
declare @password varchar(100),@telphone varchar(100) ,@mobilephone varchar(100) , @address varchar(100)
declare @user_key int 
 
set @company_key = '{company_key}'
set @user_id ='{user_id}' 
set @user_name = '{user_name}'
set @sex = '{sex}'
set @telphone ='{telphone}' 
set @mobilephone = '{mobilephone}'
set @address = '{address}'


--校验信息
if isnull(@user_id,'')=''
begin 
	RAISERROR ('SQL:用户id不允许为空', 16 ,1 ) ; 
	return 
end 

if isnull(@user_name,'')=''
begin 
	RAISERROR ('SQL:用户名称不允许为空', 16 ,1 ) ; 
	return 
end 

if EXISTS(SELECT * FROM y_user WHERE [user_id]=@user_id )
begin
	RAISERROR ('SQL:用户id已经存在', 16 ,1 ) ; 
	return 
end

if @mobilephone <> @user_id
begin
	set @password='123'
end 
else
begin
	set @password=right(@user_id,4)
end   

INSERT INTO [dbo].[y_user]([company_key],[user_id],[user_name],[simple_code],[sex],[password],[telphone],[mobilephone],[address],[create_time],[update_time])
VALUES( @company_key  , @user_id , @user_name,dbo.f_getpy(@user_name), @sex , @password,@telphone , @mobilephone, @address , getdate(), getdate() )

SET @user_key=@@IDENTITY
if  not exists(SELECT 1 FROM dbo.y_role_user WHERE user_key=@user_key)
begin
	INSERT INTO y_role_user(user_key,role_key,create_time,update_time) VALUES(@user_key,7,GETDATE(),GETDATE())
end 